package com.da.javatest.leetCode;

/**
 * @author chenlida
 * @date 2019/1/16 15:07
 * @description
 */
public class Code606 {
    private String s = "";

    public String tree2str(TreeNode t) {
        setString(t);
        s = s.replace("()", "").replace("((", "(");
        s += ")";
        return s.substring(1);
    }

    private void setString(TreeNode node) {
        if (node != null) {
            s += "(" + node.val + "(";
            if (node.left == null && node.right != null) {
                s += "()";
            }
            setString(node.left);
            s += ")";
            setString(node.right);
        }
    }
}
